package lab3.client;

import java.awt.EventQueue;

import javax.swing.JOptionPane;

import lab3.client.core.Options;
import lab3.client.view.MainFrame;

/**
 * Client application entry
 * This is designed to not contain any cyclic package
 * dependencies.
 * 
 * NOTE: Application needs a policy file in
 * project directory, see security.policy
 *
 * @author hajo
 * 
 */

// DON'T TOUCH BELOW THIS -----------------------------------
public class Main {

   private static final int N_ARGS = 5;

   /**
    * @param 
    * args[0], username 
    * args[1], password 
    * args[2], IP to registry 
    * args[3], registryport 
    * args[5], port for this client (server uses) 
    * 
    */
   public static void main(String[] args) {
      // Should have when using RMI
      if (System.getSecurityManager() == null) {
         System.setSecurityManager(new SecurityManager());
      }
      
      if (args.length == N_ARGS) {
         Options.setUser(args[0], args[1]);
         try {
            int registryPort = Integer.valueOf(args[3]);
            int myPort = Integer.valueOf(args[4]);
            Options.setConnection(args[2], registryPort, myPort);
         } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null,
                  "Bad port(s), must set in Options");
            Options.setConnection("127.0.0.1", 0, 0);
         }
      } else {
         Options.setUser("default", "default");
         Options.setConnection("127.0.0.1", 0, 0);
      }

      // Because of Swing
      EventQueue.invokeLater(new Runnable() {

         public void run() {
            new MainFrame().setVisible(true);
         }
      });
   }
}
